<template>
  <div class="container">
    <el-card>
      <!-- 提交表单 -->
      <el-row>
        <el-col :span="22">
          <el-form label-width="80px">
            <el-form-item label="关键字">
              <el-input
                style="width:215px"
                @change="searchFunction"
                v-model="search.keyword"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="search.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-left:-70px">
              <el-button size="small" @click="clearSearch">清除</el-button>
              <el-button type="primary" size="small" @click="searchFunction"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="openNewArticle"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <!-- 警告 -->
      <el-alert
        :title="`数据一共${this.counts}条`"
        type="info"
        :closable="false"
        show-icon
      >
      </el-alert>
      <!-- 表格 -->
      <el-table :data="tableData" style="width:100%">
        <el-table-column label="序号" width="100" type="index">
        </el-table-column>
        <el-table-column label="文章标题" width="400">
          {{ tableData.title }}
          <template slot-scope="scope">
            {{ scope.row.title }}
            <a
              v-if="scope.row.videoURL"
              href="javascript:;"
              class="el-icon-film"
              style="color:#00f"
              @click="videoDialog(scope.row.videoURL)"
            ></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"> </el-table-column>
        <el-table-column prop="username" label="录入人" width="120px">
        </el-table-column>
        <el-table-column label="录入时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            {{ scope.row.state == 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="previewArticle(scope.row.id)"
              >预览</el-button
            >
            <el-button type="text" @click="disableArticle(scope.row)">
              <span>{{ scope.row.state == 1 ? '启用' : '禁用' }}</span>
            </el-button>
            <el-button
              :disabled="scope.row.state == 1 ? true : false"
              type="text"
              @click="openEditArticle(scope.row)"
              >修改</el-button
            >
            <el-button
              :disabled="scope.row.state == 1 ? true : false"
              type="text"
              @click="openDeleteArticle(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[3, 5, 7, 9]"
        :page-size="5"
        layout="sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </el-card>
    <!-- 新增文章对话框 -->
    <el-dialog
      v-if="addArticleDialogVisible"
      title="新增文章"
      :visible.sync="addArticleDialogVisible"
      width="800px"
    >
      <el-form
        :model="addArticleForm"
        :rules="formRules"
        ref="addRuleFormRef"
        label-width="80px"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input
            style="width:660px"
            v-model="addArticleForm.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            style="width:660px"
            v-model="addArticleForm.articleBody"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" class="video-url">
          <el-input
            style="width:660px"
            v-model="addArticleForm.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addArticleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="newArticle">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      v-if="editArticleDialogVisible"
      title="修改文章"
      :visible.sync="editArticleDialogVisible"
      width="800px"
    >
      <el-form
        :model="editArticleForm"
        :rules="formRules"
        ref="editRuleFormRef"
        label-width="80px"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input
            style="width:660px"
            v-model="editArticleForm.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            v-model="editArticleForm.articleBody"
            style="width:660px"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" class="video-url">
          <el-input
            style="width:660px"
            v-model="editArticleForm.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editArticleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="closeEditArticle">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预览对话框 -->
    <el-dialog
      title="预览文章"
      :visible.sync="previewArticleDialogVisible"
      width="800px"
      :data="tableData"
    >
      <h2 style="margin:-10px 0 5px -10px">
        {{ this.previewArticleDetal.title }}
      </h2>
      <div style="padding:10px 0">
        {{ this.previewArticleDetal.createTime | parseTimeByString }}
        <span style="margin:0 5px">超级管理员</span>
        <span class="el-icon-view"> </span>
        {{ this.previewArticleDetal.visits }}
      </div>
      <div
        v-html="this.previewArticleDetal.articleBody"
        style="background-color:#f5f5f5;padding:10px;border-top:1px dashed #ccc"
      ></div>
    </el-dialog>
    <!-- 视频弹出框 -->
    <el-dialog
      class="video"
      :visible.sync="showVideoDialogVisible"
      :show-close="false"
      width="800px"
    >
      <div class="x" @click="closeVideoDialog">X</div>
      <video :src="videoURL" autoplay width="760px" height="540px"></video>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  remove,
  detail,
  add,
  update,
  changeState
} from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      addArticleDialogVisible: false,
      previewArticleDialogVisible: false,
      editArticleDialogVisible: false,
      showVideoDialogVisible: false,
      formRules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      addArticleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      editArticleForm: {
        title: '',
        articleBody: '',
        videoURL: '',
        id: ''
      },
      queryInfo: {
        page: 1,
        pagesize: 5
      },
      search: {
        keyword: '',
        state: ''
      },
      aboutState: {
        id: '',
        state: ''
      },
      tableData: [],
      previewArticleDetal: {},
      videoURL: '',
      counts: 0,
      username: ''
    }
  },
  created() {
    this.loadArticleList()
  },
  methods: {
    // 打开添加文章弹框
    openNewArticle() {
      this.addArticleDialogVisible = true
    },
    // 点击添加文章按钮
    newArticle() {
      this.$refs.addRuleFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项！')
        }
        await add(this.addArticleForm)
        this.addArticleForm = {}
        this.addArticleDialogVisible = false
        this.loadArticleList()
      })
    },
    // 打开编辑文章弹框
    async openEditArticle(index) {
      this.editArticleDialogVisible = true
      const { data } = await detail({ id: index.id })
      this.editArticleForm.articleBody = data.articleBody
      this.editArticleForm.videoURL = data.videoURL
      this.editArticleForm.title = data.title
      this.editArticleForm.id = data.id
    },
    // 点击编辑文章按钮
    closeEditArticle() {
      this.$refs.editRuleFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项！')
        }
        await update(this.editArticleForm)
        this.loadArticleList()
        this.$message.success('操作成功！')
        this.editArticleForm = {}
        this.editArticleDialogVisible = false
      })
    },
    // 加载文章列表
    async loadArticleList() {
      const { data } = await list(this.queryInfo)
      this.queryInfo.pagesize = data.pagesize
      this.queryInfo.page = data.page
      this.tableData = data.items
      this.counts = data.counts
      this.state = data.state
    },
    // 一个页面可以显示文章的数目(页面大小)发生改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.loadArticleList()
    },
    // 页面数值发生改变
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.loadArticleList()
    },
    // 预览文章
    async previewArticle(id) {
      const data = await detail({ id })
      console.log(data)
      this.previewArticleDetal = data.data
      this.previewArticleDialogVisible = true
    },
    // 禁用文章
    async disableArticle(index) {
      const state = index.state === 1 ? '0' : '1'
      await changeState({ id: index.id, state })
      this.loadArticleList()
    },
    // 删除文章
    async openDeleteArticle(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
      if (confirmResult !== 'confirm') {
        return this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }
      await remove({ id })
      this.loadArticleList()
    },
    // 搜索功能
    async searchFunction() {
      const { data } = await list(this.search)
      this.tableData = data.items
    },
    // 清空搜索
    clearSearch() {
      this.search.keyword = ''
      // this.$refs['searchRef'].resetFields()
      this.search.state = null
      this.searchFunction()
    },
    // 视频
    videoDialog(index) {
      this.showVideoDialogVisible = true
      this.videoURL = index
    },
    // 关闭视频弹框
    closeVideoDialog() {
      this.showVideoDialogVisible = false
      this.videoURL = ''
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .video {
  margin-left: 50px;
  .el-dialog__header {
    background-color: rgba(0, 0, 0, 0);
  }
  .el-dialog {
    background-color: rgba(0, 0, 0, 0);
    border: unset;
    box-shadow: unset;
  }
}
.el-card {
  width: 99%;
  border-radius: 4px;
  margin: 10px 10px 20px 10px;
  .el-form {
    display: flex;
    align-items: center;
  }
}
.el-alert {
  margin: 0 0 20px 0;
}
.el-pagination {
  margin: 20px 0;
  float: right;
}
.x {
  position: absolute;
  top: 0;
  left: 48%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  width: 50px;
  height: 50px;
  color: #fff;
  background-color: #ccc;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 50%;
  cursor: pointer;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-right: 20px;
  margin-top: -40px;
}
.quill-editor {
  width: 500px;
  height: 300px;
  .ql-snow {
    overflow: auto;
  }
}

.video-url {
  margin-top: 120px;
}
</style>
